<template>
  <div>
    <div class="recharge">
      <div class="top">
        <router-link
          tag="span"
          to="/recharge"
          class="iconfont icon-guanbi"
        ></router-link>
        <p>充值</p>
      </div>
      <div class="bg"></div>

      <div class="recharge_account">
        <p>充值账号</p>
        <p>书友20190903003714416</p>
      </div>

      <div class="bg"></div>

      <div class="recharge_item">
        <div>
          <i></i>
          <p>支付宝</p>
        </div>
        <span class="iconfont icon-qianjin"></span>
      </div>

      <div class="bg"></div>
      <div class="bg"></div>

      <div class="money_lists">
        <div
          class="money_item"
          v-for="item in money"
          :key="item.id"
          @click="moneyid(item.id)"
        >
          <p>
            <span>{{ item.num * 100 }}</span>
            <span>虎币</span>
          </p>
          <p>
            <span>￥{{ item.num }}</span>
            <span
              class="iconfont icon-zhengque"
              v-if="item.id == money_id"
            ></span>
          </p>
        </div>
      </div>

      <div class="custom">
        <p>
          <span>{{ custom * 100 }}虎币</span>
        </p>
        <p>
          <span>自定义金额:</span>
          <input
            type="text"
            v-model="custom"
            @keyup.enter="modify"
            @click="reset"
          />
        </p>
      </div>
      <div type="primary" @click.stop="Dialog">
        <div class="payment">
          <p>
            立即支付<span>￥{{ money_num }}</span>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      money: [
        {
          id: 1,
          num: 10,
        },
        {
          id: 2,
          num: 20,
        },
        {
          id: 3,
          num: 30,
        },
        {
          id: 4,
          num: 50,
        },
        {
          id: 5,
          num: 100,
        },
      ],
      money_id: 1,
      custom: 1,
      money_num: 10,
    };
  },
  methods: {
    moneyid(id) {
      this.money_id = id;
      let index = this.money.findIndex((item) => item.id == id);
      this.money_num = this.money[index].num;
    },
    reset() {
      this.money_num = null;
      this.money_id = null;
    },
    modify(event) {
      this.money_num = event.target.value;
    },
    payment_coin() {
      window.localStorage.coin =
        parseInt(window.localStorage.coin) + 100 * this.money_num;
      this.money_num = 10;
      this.money_id = 1;
    },
    Dialog() {
      this.$dialog
        .confirm({
          title: "提醒",
          message: "确认充值吗？",
        })
        .then(() => {
          window.localStorage.coin =
            parseInt(window.localStorage.coin) + 100 * this.money_num;
          this.money_num = 10;
          this.money_id = 1;
          this.$toast("充值成功");
        })
        .catch(() => {
          console.log("取消");
        });
    },
  },
  created() {
    if (!window.localStorage.coin) {
      window.localStorage.setItem("coin", 0);
    }
  },
};
</script>

<style lang="scss" scoped>
.payment {
  background-color: #f7f7f7;
  padding: 15px;
  text-align: center;
  p {
    background: #2e9de7;
    color: #fff;
    padding: 15px;
  }
}
.custom {
  padding: 15px;
  padding-left: 0;
  margin-left: 15px;
  display: flex;
  justify-content: space-between;
  p {
    display: flex;
    align-items: center;
    input {
      border: none;
      width: 50px;
      color: black;
      font-size: 14px;
    }
  }
}

.money_item {
  padding: 15px;
  padding-left: 0;
  margin-left: 15px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #cccccc;
  p {
    &:nth-child(1) {
      span {
        font-size: 15px;
        &:nth-child(1) {
          margin-right: 3px;
        }
      }
    }
    &:nth-child(2) {
      position: relative;
      span {
        font-size: 15px;
        &:nth-child(1) {
          margin-right: 22px;
        }
        &:nth-child(2) {
          position: absolute;
          right: 0;
          top: 3px;
          color: #309ee7;
        }
      }
    }
  }
}

.recharge_item {
  padding: 0 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  & > div {
    display: flex;
    align-items: center;
    i {
      display: block;
      background: url("https://sta.gtimg.com/yuewen/pay/images/h5/sprite.0.7.png")
        no-repeat;
      width: 32px;
      height: 32px;
      background-position: 0 -32px;
      background-size: 100%;
      margin: 15px 0;
    }
    p {
      padding: 15px 0;
      font-size: 14px;
      flex: 1;
      margin-left: 10px;
    }
  }
}

.recharge_account {
  padding: 15px;
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.bg {
  width: 100%;
  height: 10px;
  background-color: #f7f7f7;
}

.recharge {
  .top {
    padding: 10px 15px;
    background: #ffffff;
    display: flex;
    align-items: center;
    span {
      color: #2e9de7;
      font-size: 18px;
    }
    p {
      flex: 1;
      color: #3d4145;
      font-size: 16px;
      text-align: center;
    }
  }
}
</style>
